import 'package:async_wallpaper/async_wallpaper.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
import 'package:wallpaper/components/empty.dart';
import 'package:wallpaper/components/z_video/zsn_video.dart';

class VideoPage extends StatefulWidget {
  const VideoPage({super.key});

  @override
  VideoPageState createState() => VideoPageState();
}

class VideoPageState extends State<VideoPage> {
  String videoUrl = '';
  Future<void> _pickVideoFromGallery() async {
    final XFile? video =
        await ImagePicker().pickVideo(source: ImageSource.gallery);
    if (video != null) {
      setState(() {
        videoUrl = video.path;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        toolbarHeight: 40,
        backgroundColor: Theme.of(context).colorScheme.primaryContainer,
        surfaceTintColor: Theme.of(context).colorScheme.primaryContainer,
        elevation: 0,
        title: Text(
          '本地视频',
          style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
        ),
        actions: [
          IconButton(
              onPressed: () {
                setState(() {
                  videoUrl = '';
                });
                _pickVideoFromGallery();
              },
              icon: Icon(Icons.add))
        ],
      ),
      floatingActionButton: FloatingActionButton(
          // 圆角
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(40),
          ),
          onPressed: () async {
            if (videoUrl.isEmpty) {
              BotToast.showText(text: '请先选择视频');
            } else {
              try {
                var result = await AsyncWallpaper.setLiveWallpaper(
                  filePath: videoUrl,
                  goToHome: false,
                  toastDetails: ToastDetails.success(),
                  errorToastDetails: ToastDetails.error(),
                );
                if (result == true) {
                  BotToast.showText(text: '设置成功');
                } else {
                  BotToast.showText(text: '设置失败', backgroundColor: Colors.red);
                }
              } on PlatformException {
                BotToast.showText(text: '设置失败', backgroundColor: Colors.red);
              }
            }
          },
          child: Icon(
            Icons.ad_units,
            color: Theme.of(context).colorScheme.primary,
          )),
      body: videoUrl.isEmpty
          ? Empty()
          : Column(
              children: [
                ZsnVideo(videoUrl: videoUrl),
                Center(
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Text(
                      '只支持本地的mp4视频',
                      style: TextStyle(color: Colors.grey, fontSize: 14),
                    ),
                  ),
                )
              ],
            ),
    );
  }
}
